# encoding:utf-8
import socket,base64,hashlib
import time

# 上传数据部分采用HTTP
def submitMeasureData(data):
    # 提交数据的url
    url = "http://localhost:5000/measure/submit/one"
    formdata = {
        "username": "ljp",
        "measureData": data
    }
    headers={
        'Content-Type': 'application/json'
    }
    print(formdata)
    response = requests.post(url, data = json.dumps(formdata), headers = headers)
    print(response.json)


# 接收上位机控制部分采用websocket，以下均是websocket部分
def get_headers(data):
    '''将请求头转换为字典'''
    header_dict = {}
    data = str(data,encoding="utf-8")

    header,body = data.split("\r\n\r\n",1)
    header_list = header.split("\r\n")
    for i in range(0,len(header_list)):
        if i == 0:
            if len(header_list[0].split(" ")) == 3:
                header_dict['method'],header_dict['url'],header_dict['protocol'] = header_list[0].split(" ")
        else:
            k,v=header_list[i].split(":",1)
            header_dict[k]=v.strip()
    return header_dict


def get_data(payload):
    payload_len = payload[1] & 127
    if payload_len == 126:
        extend_payload_len = payload[2:4]
        mask = payload[4:8]
        decoded = payload[8:]

    elif payload_len == 127:
        extend_payload_len = payload[2:10]
        mask = payload[10:14]
        decoded = payload[14:]
    else:
        extend_payload_len = None
        mask = payload[2:6]
        decoded = payload[6:]

    # 这里我们使用字节将数据全部收集，再去字符串编码，这样不会导致中文乱码
    bytes_list = bytearray()

    for i in range(len(decoded)):
        # 解码方式
        chunk = decoded[i] ^ mask[i % 4]
        bytes_list.append(chunk)
    body = str(bytes_list, encoding='utf-8')
    return body


def send_msg(conn, msg_bytes):
    """
    WebSocket服务端向客户端发送消息
    :param conn: 客户端连接到服务器端的socket对象,即： conn,address = socket.accept()
    :param msg_bytes: 向客户端发送的字节
    :return: 
    """
    import struct

    token = b"\x81" # 接收的第一字节，一般都是x81不变
    length = len(msg_bytes)
    if length < 126:
        token += struct.pack("B", length)
    elif length <= 0xFFFF:
        token += struct.pack("!BH", 126, length)
    else:
        token += struct.pack("!BQ", 127, length)

    msg = token + msg_bytes
    conn.send(msg)
    return True


def initWebSocket(startCallBack, stopCallBack):
    sock = socket.socket()
    sock.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
    sock.bind(("127.0.0.1",3000))
    sock.listen(5)
    print('等待用户连接')

    #等待用户连接
    conn,addr = sock.accept()
    print("conn from ",conn,addr)
    #获取握手消息，magic string ,sha1加密
    #发送给客户端
    #握手消息
    data = conn.recv(8096)
    headers = get_headers(data)

    # 对请求头中的sec-websocket-key进行加密
    response_tpl = "HTTP/1.1 101 Switching Protocols\r\n" \
        "Upgrade:websocket\r\n" \
        "Connection: Upgrade\r\n" \
        "Sec-WebSocket-Accept: %s\r\n" \
        "WebSocket-Location: ws://%s%s\r\n\r\n"

    magic_string = '258EAFA5-E914-47DA-95CA-C5AB0DC85B11'

    value = headers['Sec-WebSocket-Key'] + magic_string
    ac = base64.b64encode(hashlib.sha1(value.encode('utf-8')).digest())

    response_str = response_tpl % (ac.decode('utf-8'), headers['Host'], headers['url'])

    # 响应【握手】信息
    conn.send(bytes(response_str, encoding='utf-8'))

    while True:
      data = conn.recv(8096)
      data = get_data(data)
      if data == 'start Measure':
          print('startCallBackFunc running!')
          startCallBack()
      if data == 'reset Measure':
          print('stopCallBackFunc running!')
          stopCallBack()